package edu.gatech.cs2340.spring2012.team38;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseHelper
{
	private static final String DATABASE_NAME = "todolist.db";
	private static final int DATABASE_VERSION = 1;
	private static final String TABLE_USER = "user";
	
	public static final String KEY_USERNAME = "username";
    public static final String KEY_PASSWORD = "password";
    public static final String KEY_ROWID = "_id";
    
    /*private static final String CREATE_USER =
        "create table user (_id integer primary key autoincrement, "
        + "title text not null, body text not null);";*/
    private static final String CREATE_USER = "CREATE TABLE "+ TABLE_USER +
		" (username varchar(255) " + "PRIMARY KEY, " +
		"password varchar(255))";
	
	private dBHelper dBHelp;
	private SQLiteDatabase database;
	private final Context ctx;
	
	
	private static class dBHelper extends SQLiteOpenHelper {
		dBHelper (Context context){
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}
		public void onCreate(SQLiteDatabase db) {

            db.execSQL(CREATE_USER);
        }

        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w("DatabaseHelper", "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS notes");
            onCreate(db);
        }
		
	}
	
	public DatabaseHelper(Context context)
	{
		this.ctx = context;
	}
	
	public DatabaseHelper open() throws SQLException {
        dBHelp = new dBHelper(ctx);
        database = dBHelp.getWritableDatabase();
        return this;
    }
	
	public long insertUser(String username, String password)
	{
		ContentValues initialValues = new ContentValues();
        initialValues.put("username", username);
        initialValues.put("password", password);

        return database.insert(TABLE_USER, null, initialValues);
	}

	public String lookupUser(String username)
	{
		String lookupUser = "SELECT username FROM " + TABLE_USER + " WHERE username=\'?\'";
		String resultUser[] = {username};
		String userN[] = {KEY_USERNAME};
		Cursor userCur = database.rawQuery(lookupUser,  resultUser);
		//String selection = "Select username from user where username= ";
		//Cursor userCur = database.query(TABLE_USER, userN, "username=" + "'%username%'", null, null, null, null);
		return userCur.getString(0);
	}
	
	public Cursor fetchAllUsers() {

        return database.query(TABLE_USER, new String[] {KEY_ROWID, KEY_USERNAME,
                KEY_PASSWORD}, null, null, null, null, null);
    }
	
	public void closeDB()
	{
		dBHelp.close();
	}
	
}
